<?php

namespace app\foreigndata\controller;

use app\common\controller\Api;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 首页接口
 */
class Vxlogin extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $appid = 'wxbf55f392d2b133c8';
    protected $appsecret = 'dc185776d472eab6fa8db06b874a4b52';
    protected $Lifetime = 7200;


    public function __construct(){
        ini_set("session.gc_maxlifetime", $this->Lifetime);//设置session生存时间
        ini_set("session.gc_probability", "1");
        session_start();
    }

    //
    public function logins()
    {
        $url = urlencode("https://www.780g.com/foreigndata/vxlogin/back_url");
// 		return "https://open.weixin.qq.com/".
        header("Location:https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".$url."&response_type=code&scope=snsapi_login#wechat_redirect");
    }

    public function back_url(){
        $code = $_GET['code'];

        // $_SESSION['code'] = $code;
        //if(!isset($_SESSION['token'])){
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
        //获取accesstoken
        $tokenInfo = $this->curl_get($url);

        $token = $tokenInfo->access_token;
        $openid = $tokenInfo->openid;
        //将token存入缓存里
        //$_SESSION['token'] = $token;
        //}else{
        // $token = $_SESSION['token'];
        // }
        $user_info = DB::name("personal")->where('openid',$openid)->find();
        if($user_info && $user_info['mobile']){
            $token_s = Random::uuid();
            DB::name("personal")->where("openid",$openid)->update(['token'=>$token_s]);
            header("Location:https://www.780g.com/login/Logvx?openid=".$openid."&mobile=".$user_info['mobile']."&token=".$token_s);
            // header("Location:http://localhost:8080/login/Logvx?openid=".$openid."&mobile=".$user_info['mobile']."&token=".$token_s);
        }else{
            header("Location:https://www.780g.com/login/Logvx?openid=".$openid."&mobile=&token=");
            // header("Location:http://localhost:8080/login/Logvx?openid=".$openid."&mobile=&token=");
        }
        //根据token获取openid
        //$url1 = "";
        //echo $code;die;
    }
    
    //app上使用，QQ登录
    public function qqlogin(){
        // $openid = $_GET['openid'];
        $openid = $_POST['openid'];
        
        $user_info = DB::name("personal")->where('qqopenid',$openid)->find();
        if($user_info && $user_info['mobile']){
            $token_s = Random::uuid();
            DB::name("personal")->where("id",$user_info['id'])->update(['token'=>$token_s]);
            $data = array(
                'code'=>1,
                'token'=>$token_s,
                'mobile'=>$user_info['mobile']
            );
            // header("Location:http://localhost:8080/login/Logvx?openid=".$openid."&mobile=".$user_info['mobile']."&token=".$token_s);
        }else{
            $data = array(
                'code'=>0,
                'token'=>'',
                'mobile'=>''   
            );
        }
        
        echo json_encode($data);die;
        
    }
    
    public function vxlogin(){
        // $openid = $_GET['openid'];
        $openid = $_POST['openid'];
        $user_info = DB::name("personal")->where('app_vxopenid',$openid)->find();
        if($user_info && $user_info['mobile']){
            $token_s = Random::uuid();
            DB::name("personal")->where("id",$user_info['id'])->update(['token'=>$token_s]);
            $data = array(
                'code'=>1,
                'token'=>$token_s,
                'mobile'=>$user_info['mobile']
            );
            // header("Location:http://localhost:8080/login/Logvx?openid=".$openid."&mobile=".$user_info['mobile']."&token=".$token_s);
        }else{
            $data = array(
                'code'=>0,
                'token'=>'',
                'mobile'=>''   
            );
        }
        echo json_encode($data);die;
    }

    public function curl_get($url){

        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);

        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);

        // 显示错误信息
        if (curl_error($curl)) {
            print "Error: " . curl_error($curl);
        } else {
            // 打印返回的内容
            return json_decode($data);
        }
    }

    public function refreshtoken(){

    }


}
